package server;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import util.Message;
import util.Status;
import data.DataService;
import dataPO.InfoPO;

public class Connector {

    private static String IP = "localhost";
    private static int PORT = 8889;

    private static DataService getRefObject() {
        try {
            return (DataService) Naming.lookup("rmi://" + IP + ":" + PORT + "/data");

        } catch (NotBoundException | MalformedURLException | RemoteException e) {
            return null;
        }
    }

    public static Message add(InfoPO info) {
        try {

            return getRefObject().add(info);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message delete(InfoPO info) {
        try {
            return getRefObject().delete(info);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message update(InfoPO info) {
        try {
            return getRefObject().update(info);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message getCourseRecord(String userID) {
        try {
            return getRefObject().getCoursesRecord(userID);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message getCourseStudentRecord(String courseID) {
        try {
            return getRefObject().getCourseStudentRecord(courseID);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message getTeachFrameItems() {
        try {
            return getRefObject().getTeachFrameItems();

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message getCourseExtendedInfo(String courseID) {
        try {
            return getRefObject().getCourseExtendedInfo(courseID);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message getAccountInfo(String userID) {
        try {
            return getRefObject().getAccountInfo(userID);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message getCourseSelectionRequstByCourse(String courseID) {
        try {
            // TODO
            return getRefObject().getCourseRequstByCourse(courseID);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }
    
    public static Message getCourseSelectionRequstByUser(String userID) {
        try {
            // TODO
            return getRefObject().getCourseRequstByStudent(userID);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message getCourseInfo(String courseID) {
        try {
            return getRefObject().getCourseInfo(courseID);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message getUsersBy(String ID, String department, String grade, String type) {
        try {
            return getRefObject().getUsersBy(ID, department, grade, type);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message getTeachPlanItems(String department) {
        try {
            return getRefObject().getTeachPlanItems(department);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message getCoursesBy(String department, String grade, String type, String teacher) {
        try {
            return getRefObject().getCourseBy(department, grade, type, teacher);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static int getCourseStudentNumber(String courseID) {
        Message info = getCourseStudentRecord(courseID);
        try {
            return info.getItemList().length;

        } catch (NullPointerException e) {
            return -1;
        }
    }

    public static Message getCoursesByKey(String s) {
        try {
            return getRefObject().getCourseByKey(s);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }

    public static Message getUsersByKey(String s) {
        try {
            return getRefObject().getAccountByKey(s);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
    }
}
